﻿
Partial Class WebForm_CustomerGroupPercentageQuery
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            InitDataAndShow()
        End If
    End Sub

    Public Sub InitDataAndShow()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim hmManager As New HierarchyManager
        Dim dt As New DataTable

        Me.IsChain = "Y"

        sql.Length = 0
        sql.AppendFormat(" Select Chain from ProductLevel where LevelName='{0}' ", Me.Level2Name)
        dt = dat.GetData(sql.ToString())
        If dt.Rows.Count > 0 Then
            Me.IsChain = dt.Rows(0)("Chain").ToString()
        End If

        'Search 條件
        sql.Length = 0
        sql.AppendFormat(" SELECT Distinct A.Division,'" & Me.BasicSignature & "' as Signature, C.Description as Level1Name, D.Description as Level2Name ")
        If Me.IsChain = "Y" Then
            sql.AppendFormat(",D.Code as Level2Value ")
        Else
            sql.AppendFormat(",substring(D.Code,{0}*2-1,2) as Level2Value ", hmManager.GetSequence(Me.Level2Name))
        End If
        sql.AppendFormat(" ,A.Channel, A.ChannelName, (CAST( B.Percentage AS varchar(50))+'%') AS Percentage, B.Percentage AS iPercentage   ")
        sql.AppendFormat(" FROM ChannelView A LEFT JOIN ChannelPercentage B ")
        sql.AppendFormat(" ON A.[Signature] =  Left(B.[Signature],2) AND A.Channel = B.Channel ")
        sql.AppendFormat(" AND B.Signature ='{0}' ", Me.BasicSignature)
        If Me.Level2Value <> "" Then
            sql.AppendFormat(" AND B.Level2Name='{0}' ", Me.Level2Name)
        Else
            sql.AppendFormat(" AND B.Level2Name='{0}' ", "")
        End If
        sql.AppendFormat(" AND B.Level2Value ='{0}' ", Me.Level2Value)
        sql.AppendFormat(" LEFT JOIN  ProductHierarchyOriginal C ")
        sql.AppendFormat(" ON A.Signature = C.CODE AND C.LevelName='Signature' ")
        sql.AppendFormat(" LEFT JOIN  ProductHierarchyOriginal D ")
        sql.AppendFormat(" ON D.LevelName='{0}' ", Me.Level2Name)
        If Me.IsChain = "Y" Then
            sql.AppendFormat(" AND D.CODE='{0}' ", Me.Level2Value)
        Else
            sql.AppendFormat(" AND substring(D.code,{0}*2-1,2)='{1}' ", hmManager.GetSequence(Me.Level2Name), Me.Level2Value)
            sql.AppendFormat(" AND A.Signature = Left(D.CODE,2) ")
        End If
        sql.AppendFormat(" WHERE 1=1 ")
        sql.AppendFormat(" AND A.Signature ='{0}' ", Me.Signature)
        sql.AppendFormat(" Order by A.Division, C.Description, A.ChannelName ")
        'MyObj.WriteLog(sql.ToString())

        dt = dat.GetData(sql.ToString())

        For i As Integer = 0 To dt.Rows.Count - 1
            dt.Rows(i)("Level1Name") = Me.SignatureText
        Next

        Me.gv.Columns(1).HeaderText = Me.Level2Name
        Me.gv.DataSource = dt
        Me.gv.DataBind()

    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.InitDataAndShow()
    End Sub

    Private ReadOnly Property Signature() As String
        Get
            Return Session(Me.SessionID & "SF_Signature")
        End Get
    End Property

    Private ReadOnly Property BasicSignature() As String
        Get
            Return Session(Me.SessionID & "SF_BasicSignature")
        End Get
    End Property

    Private ReadOnly Property SignatureText() As String
        Get
            Return Session(Me.SessionID & "SF_SignatureText")
        End Get
    End Property

    Private ReadOnly Property IsCatagory() As Boolean
        Get
            Return Session(Me.SessionID & "SF_IsCatagory")
        End Get
    End Property

    Private Property IsChain()
        Get
            Return ViewState("IsChain")
        End Get
        Set(ByVal value)
            ViewState("IsChain") = value
        End Set
    End Property

    Private Property Level2Name()
        Get
            Return Session(Me.SessionID & "SF_SecondLevelName")
        End Get
        Set(ByVal value)
            Session(Me.SessionID & "SF_SecondLevelName") = value
        End Set
    End Property

    Private Property Level2Value()
        Get
            Return Session(Me.SessionID & "SF_SecondLevelValue")
        End Get
        Set(ByVal value)
            Session(Me.SessionID & "SF_SecondLevelValue") = value
        End Set
    End Property

    Public ReadOnly Property SessionID() As String
        Get
            Return Me.Request.QueryString.Item("SessionID")
        End Get
    End Property
End Class
